﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace TournamentDatabase.Database
{
    class DBConnection : IDisposable
    {
        private Process p;
        bool connected;
        public DBConnection()
        {
            // Start the child process.
            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.FileName = @"C:\apps\Oracle\bin\sqlplus.exe";
            p.StartInfo.Arguments = @"group10/AzT4hMYV@TEACHING";
            connected = false;
        }
        public void Connect()
        {
            if (connected) return;
            connected = true;
            p.Start();
            p.StandardInput.WriteLine(
                @"
                set echo off
                set feedback off
                set linesize 300
                set pagesize 0
                set sqlprompt ''
                set trimspool on");
        }

        public void Dispose()
        {
            p.StandardInput.WriteLine("exit");

        }
        public void MakeQuery(string str)
        {



        }
        public OracleDataReader MakeQuery(string str, string[] column)
        {
           


        }
        public OracleDataReader ParseResults(string[] column,Stream s)
        {
            OracleDataReader odr = new OracleDataReader(column);
            int i = 0;
            string[] lines = File.ReadAllLines("TextMember.txt");

            string[] firstline = lines[10].Split(',');
            if (column.Length != firstline.Length) throw new Exception("Wrong sized data values");

            firstline[0] = firstline[0].Split(' ')[firstline[0].Split(' ').Length - 1];
            string[] dp = new string[column.Length];

            Array.Copy(firstline, dp, column.Length);
            odr.Add(dp);

            try
            {
                for ( i=i+1-1; i < lines.Length; i++)
                {
                    Array.Copy(lines[i].Split(','), dp, column.Length);
                    odr.Add(dp);
                }
            }
            catch (ArgumentException)
            { }

            return odr;
        }
    }

}
